{% extends 'base.html' %}

{% block title %}所有工单 - 维修管理系统{% endblock %}

{% block content %}
<div class="container">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2>所有维修工单</h2>
        <span class="badge bg-primary">
            管理员: {{ user.username }}
        </span>
    </div>

    {% if messages %}
    <div class="alert alert-info">
        {% for message in messages %}
        <div>{{ message }}</div>
        {% endfor %}
    </div>
    {% endif %}

    {% if tickets %}
    <div class="table-responsive">
        <table class="table table-striped table-hover">
            <thead class="table-dark">
                <tr>
                    <th>工单号</th>
                    <th>工单标题</th>
                    <th>状态</th>
                    <th>优先级</th>
                    <th>创建者</th>
                    <th>分配技术人员</th>
                    <th>创建时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>
                {% for ticket in tickets %}
                <tr>
                    <td>
                        {% if ticket.ticket_number %}
                        <strong>{{ ticket.ticket_number }}</strong>
                        {% else %}
                        <span class="text-muted">-</span>
                        {% endif %}
                    </td>
                    <td>
                        <strong>{{ ticket.title }}</strong>
                        {% if ticket.assets.exists %}
                        <br>
                        <small class="text-muted">
                            设备: 
                            {% for asset in ticket.assets.all %}
                            <span class="badge bg-light text-dark me-1">{{ asset.code }}</span>
                            {% endfor %}
                        </small>
                        {% endif %}
                    </td>
                    <td>
                        <span class="badge 
                            {% if ticket.status == 'draft' %}bg-secondary
                            {% elif ticket.status == 'applied' %}bg-warning
                            {% elif ticket.status == 'registered' %}bg-info
                            {% elif ticket.status == 'assigned' %}bg-primary
                            {% elif ticket.status == 'testing' %}bg-info
                            {% elif ticket.status == 'repairing' %}bg-warning
                            {% elif ticket.status == 'quality_check' %}bg-info
                            {% elif ticket.status == 'completed' %}bg-success
                            {% elif ticket.status == 'returned' %}bg-success
                            {% else %}bg-light text-dark{% endif %}">
                            {{ ticket.get_status_display }}
                        </span>
                    </td>
                    <td>
                        <span class="badge 
                            {% if ticket.priority == 'high' %}bg-danger
                            {% elif ticket.priority == 'medium' %}bg-warning
                            {% else %}bg-info{% endif %}">
                            {{ ticket.get_priority_display }}
                        </span>
                    </td>
                    <td>{{ ticket.created_by.username }}</td>
                    <td>
                        {% if ticket.assigned_to %}
                        {{ ticket.assigned_to.username }}
                        {% else %}
                        <span class="text-muted">未分配</span>
                        {% endif %}
                    </td>
                    <td>{{ ticket.created_at|date:"Y-m-d H:i" }}</td>
                    <td>
                        <div class="btn-group">
                            <a href="{% url 'tickets:ticket_detail' ticket.pk %}" class="btn btn-sm btn-outline-primary">
                                详情
                            </a>
                            {% if not ticket.assigned_to and ticket.status == 'registered' %}
                            <a href="{% url 'tickets:ticket_assign' ticket.pk %}" class="btn btn-sm btn-outline-success">
                                分配
                            </a>
                            {% endif %}
                        </div>
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
    {% else %}
    <div class="text-center py-5">
        <div class="text-muted">
            <i class="bi bi-inbox" style="font-size: 3rem;"></i>
            <h4 class="mt-3">暂无工单</h4>
            <p>系统中还没有任何维修工单</p>
        </div>
    </div>
    {% endif %}
</div>

<style>
.container {
    max-width: 1600px;
    margin: 20px auto;
}
.table th {
    background-color: #2c3e50;
    color: white;
}
.badge {
    font-size: 0.85em;
    padding: 0.5em 0.75em;
}
.btn {
    border-radius: 5px;
}
.alert {
    border-radius: 5px;
    margin-bottom: 20px;
}
.table-hover tbody tr:hover {
    background-color: rgba(52, 152, 219, 0.1);
}
.btn-group .btn {
    margin-right: 5px;
}
</style>
{% endblock %}
